package com.own.business.pay.entity.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.own.business.pay.common.constant.ConstantPay;
import com.own.business.pay.common.exception.PayException;
import com.own.component.common.base.entity.po.BaseBean;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * BillHistory
 *
 * @author chenxueli
 * @date 2023-08-28 17:14:22
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "own_bill_history")
public class BillHistory extends BaseBean implements Serializable {

    /**
     * 用户id
     */
    private Long fkUserId;

    /**
     * 交易类型（wechat、alipay、balance...）
     */
    private String tradeType;

    /**
     * 流水类型（1=收入 2=支出 3=退款）
     */
    private Integer type;

    /**
     * 商户单号
     */
    private String outTradeNo;

    /**
     * 业务类型
     */
    private String businessType;

    /**
     * 商户退款单号
     */
    private String outRefundNo;

    /**
     * 订单标题
     */
    private String subject;

    /**
     * 订单附加信息
     */
    private String body;

    /**
     * 订单总金额，单位为分
     */
    private Long totalAmount;

    /**
     * 退款金额，单位为分
     */
    private Long refundAmount;

    /**
     * 订单超时时间
     */
    private LocalDateTime expireTime;

    /**
     * 订单状态（1=新建 2=完成 4=订单关闭 5=超时取消 6=已退款（全额） 7=已退款（部分）
     */
    private Integer status;

    /**
     * 交易时间
     */
    private LocalDateTime tradeTime;

    /**
     * 通知数据
     */
    private String notifyData;

    public String canRefundMessage() {
        if (status == null) {
            return "订单状态异常";
        }
        if (status == ConstantPay.Status.NEW_BUILT) {
            return "订单未支付";
        }
        if (status == ConstantPay.Status.COMPLETE_CLOSE) {
            return "订单已关闭";
        }
        if (status == ConstantPay.Status.COMPLETE_REFUND) {
            return "订单已退款";
        }
        if (status == ConstantPay.Status.COMPLETE_TIMEOUT) {
            return "订单已超时取消";
        }
        return null;
    }

    public void canRefund() {
        var message = canRefundMessage();
        if (message != null) {
            throw new PayException(message);
        }
    }

}
